<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item :label="t('ustcStudentInfoAdmission.fields.accountId')" prop="accountId">
        <el-input
          v-model="formData.accountId"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoAdmission.fields.accountId')
          "
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoAdmission.fields.admissionsYear')"
        prop="admissionsYear"
      >
        <el-input
          v-model="formData.admissionsYear"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoAdmission.fields.admissionsYear')
          "
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoAdmission.fields.candidateNumber')"
        prop="candidateNumber"
      >
        <el-input
          v-model="formData.candidateNumber"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoAdmission.fields.candidateNumber')
          "
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoAdmission.fields.admissionCategory')"
        prop="admissionCategory"
      >
        <el-input
          v-model="formData.admissionCategory"
          :placeholder="
            t('common.inputTextPlaceholder') +
            t('ustcStudentInfoAdmission.fields.admissionCategory')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoAdmission.fields.examMethod')" prop="examMethod">
        <el-input
          v-model="formData.examMethod"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoAdmission.fields.examMethod')
          "
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoAdmission.fields.retentionPeriod')"
        prop="retentionPeriod"
      >
        <el-input
          v-model="formData.retentionPeriod"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoAdmission.fields.retentionPeriod')
          "
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoAdmission.fields.trainingUnitLocation')"
        prop="trainingUnitLocation"
      >
        <el-input
          v-model="formData.trainingUnitLocation"
          :placeholder="
            t('common.inputTextPlaceholder') +
            t('ustcStudentInfoAdmission.fields.trainingUnitLocation')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoAdmission.fields.trainingUnit')" prop="trainingUnit">
        <el-input
          v-model="formData.trainingUnit"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoAdmission.fields.trainingUnit')
          "
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoAdmission.fields.rewardsSanctions')"
        prop="rewardsSanctions"
      >
        <el-input
          v-model="formData.rewardsSanctions"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoAdmission.fields.rewardsSanctions')
          "
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoAdmission.fields.personalExperience')"
        prop="personalExperience"
      >
        <el-input
          v-model="formData.personalExperience"
          :placeholder="
            t('common.inputTextPlaceholder') +
            t('ustcStudentInfoAdmission.fields.personalExperience')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoAdmission.fields.familyInfo')" prop="familyInfo">
        <el-input
          v-model="formData.familyInfo"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoAdmission.fields.familyInfo')
          "
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading"
        >{{ t('common.ok') }}
      </el-button>
      <el-button @click="dialogVisible = false">{{ t('common.cancel') }}</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import {
  UstcStudentInfoAdmissionApi,
  UstcStudentInfoAdmissionVO
} from '@/api/core/ustcstudentinfoadmission'
import { FormRules } from 'element-plus'

/** 学生入学信息 表单 */
defineOptions({ name: 'StudentInfoAdmissionForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  accountId: undefined,
  admissionsYear: undefined,
  candidateNumber: undefined,
  admissionCategory: undefined,
  examMethod: undefined,
  retentionPeriod: undefined,
  trainingUnitLocation: undefined,
  trainingUnit: undefined,
  rewardsSanctions: undefined,
  personalExperience: undefined,
  familyInfo: undefined
})
const formRules = reactive<FormRules>({
  id: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoAdmission.fields.id'),
      trigger: 'blur'
    }
  ],
  accountId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoAdmission.fields.accountId'),
      trigger: 'blur'
    }
  ],
  admissionsYear: [
    {
      required: true,
      message:
        t('common.inputTextPlaceholder') + t('ustcStudentInfoAdmission.fields.admissionsYear'),
      trigger: 'blur'
    }
  ],
  candidateNumber: [
    {
      required: true,
      message:
        t('common.inputTextPlaceholder') + t('ustcStudentInfoAdmission.fields.candidateNumber'),
      trigger: 'blur'
    }
  ],
  admissionCategory: [
    {
      required: true,
      message:
        t('common.inputTextPlaceholder') + t('ustcStudentInfoAdmission.fields.admissionCategory'),
      trigger: 'blur'
    }
  ],
  examMethod: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoAdmission.fields.examMethod'),
      trigger: 'blur'
    }
  ],
  retentionPeriod: [
    {
      required: true,
      message:
        t('common.inputTextPlaceholder') + t('ustcStudentInfoAdmission.fields.retentionPeriod'),
      trigger: 'blur'
    }
  ],
  trainingUnitLocation: [
    {
      required: true,
      message:
        t('common.inputTextPlaceholder') +
        t('ustcStudentInfoAdmission.fields.trainingUnitLocation'),
      trigger: 'blur'
    }
  ],
  trainingUnit: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoAdmission.fields.trainingUnit'),
      trigger: 'blur'
    }
  ],
  rewardsSanctions: [
    {
      required: true,
      message:
        t('common.inputTextPlaceholder') + t('ustcStudentInfoAdmission.fields.rewardsSanctions'),
      trigger: 'blur'
    }
  ],
  personalExperience: [
    {
      required: true,
      message:
        t('common.inputTextPlaceholder') + t('ustcStudentInfoAdmission.fields.personalExperience'),
      trigger: 'blur'
    }
  ],
  familyInfo: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoAdmission.fields.familyInfo'),
      trigger: 'blur'
    }
  ],
  creator: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoAdmission.fields.creator'),
      trigger: 'blur'
    }
  ],
  createTime: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoAdmission.fields.createTime'),
      trigger: 'blur'
    }
  ],
  updater: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoAdmission.fields.updater'),
      trigger: 'blur'
    }
  ],
  updateTime: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoAdmission.fields.updateTime'),
      trigger: 'blur'
    }
  ],
  deleted: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoAdmission.fields.deleted'),
      trigger: 'blur'
    }
  ],
  tenantId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoAdmission.fields.tenantId'),
      trigger: 'blur'
    }
  ]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await UstcStudentInfoAdmissionApi.getUstcStudentInfoAdmission(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as UstcStudentInfoAdmissionVO
    if (formType.value === 'create') {
      await UstcStudentInfoAdmissionApi.createUstcStudentInfoAdmission(data)
      message.success(t('common.createSuccess'))
    } else {
      await UstcStudentInfoAdmissionApi.updateUstcStudentInfoAdmission(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    accountId: undefined,
    admissionsYear: undefined,
    candidateNumber: undefined,
    admissionCategory: undefined,
    examMethod: undefined,
    retentionPeriod: undefined,
    trainingUnitLocation: undefined,
    trainingUnit: undefined,
    rewardsSanctions: undefined,
    personalExperience: undefined,
    familyInfo: undefined
  }
  formRef.value?.resetFields()
}
</script>
